//
//  Navigator.m
//  Pocket Doctor alpha version
//
//  Created by Sherif, Yin, Taylor, Jacob, Anna, Mike on 11-10-25.
//  Copyright (c) 2011 __Sytham__. All rights reserved.
//
//Class Description: Class handles which view to go to; 
//                   if database is present; go to main menu
//                   if no database is found.. go to first time launch.
//
//Known Bugs: none.
//

#import "Navigator.h"
#import "AppDelegate.h"
#import "FirstTimeLaunch.h"
#import "MainMenu.h"


@implementation Navigator

@synthesize ftl,mm, aD;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView
 {
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    
    //check database if application ran before.
    //for development purpose; the screen will run everytime.
    //if(database is false)
    
    aD = [[UIApplication sharedApplication] delegate];
    [aD initDB];
    
    sqlite3_stmt * statement;
    NSString * enforceForeignKey = @"PRAGMA foreign_keys = 1";
    const char * query_stmt = [enforceForeignKey UTF8String];
    
    NSString *loadSecurity = [[NSString alloc]initWithFormat:@"SELECT passcode, passcode_hint, secret_question, secret_answer FROM security;"];    
    query_stmt = [loadSecurity UTF8String];
    if (sqlite3_prepare_v2(aD.dbPointer, query_stmt, -1, &statement, NULL) == SQLITE_OK){
        
        if (sqlite3_step(statement) == SQLITE_ROW){
            //[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
            // Row Exists for security. advance to main menu
            MainMenu *mmObject = [[MainMenu alloc]initWithNibName:@"MainMenu" bundle:nil];
            self.mm = mmObject;
            [mmObject release];
            
            [self.view addSubview:mm.view];
        }
        else{
            // row does not exist for security, advance to first time launch.
            FirstTimeLaunch *ftlObject = [[FirstTimeLaunch alloc]initWithNibName:@"FirstTimeLaunch" bundle:nil];
            self.ftl = ftlObject;
            
            [ftlObject release];
            
            [self.view addSubview:ftl.view];
        }
    }
    
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc{
    
    [ftl release];
    [super dealloc];
}

@end